package com.example.service;

import cn.hutool.core.date.DateUtil;
import com.example.entity.Business;
import com.example.entity.Collect;
import com.example.entity.History;
import com.example.mapper.HistoryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class HistoryService {

    @Resource
    HistoryMapper historyMapper;
    @Resource
    BusinessService businessService;

    /**
     * 查询全部
     * @param history
     * @return
     */
    public List<History> selectAll(History history){
        List<History> historyList = historyMapper.selectAll(history);
//        添加business的值
        for (History h : historyList) {
            Business business = businessService.selectById(h.getBusinessId());
            h.setBusiness(business);
        }
        return historyList;
    }

    /**
     * 修改历史数据
     */
    public void update(History history) {
        List<History> historyList = this.selectAll(history);
        history.setTime(DateUtil.now());
        if( historyList.size() != 0){
            History dbHistory = historyList.get(0);
            history.setId(dbHistory.getId());
            historyMapper.updateById(history);
        }else{
            historyMapper.insert(history);
        }
    }

    /**
     * 根据id查询
     */
    public History selectById(Integer id){
        return historyMapper.selectById(id);
    }

    /**
     * 删除
     */
    public void delete(Integer id) {

        historyMapper.deleteById(id);
    }
}
